		include	Common.asm

; ==========================================================
DataSeg		segment para public 'data'

	Seed		dw	0
	RandA		db	33
	RandB		db	127

	RandomBase 	dw	00F16h, 0F0C1h, 0D164h, 01241h, 00B81h, 05FC0h
			dw	01829h, 00A1Ah, 0F6C4h, 04F5Fh, 0B115h, 08F0Fh
			dw	01A72h, 03220h, 059BAh, 0B698h, 095D3h, 06B82h
			dw	0806Eh, 0B08Bh, 05BF2h, 04B74h, 02DE5h, 01FE2h
			dw	0DD30h, 0DC83h, 0BE73h, 0A571h, 0867Dh, 031DCh
			dw	02FFBh, 0F601h, 0EBAAh, 0E7D6h, 0C350h, 0A2BDh
			dw	06321h, 0D501h, 091CDh, 0A765h, 0A968h, 0759Eh
			dw	0488Ch, 0BCEBh, 0166Eh, 0272Eh, 00A05h, 07894h
			dw	06386h, 0B82Bh, 0839Ch, 07DAFh, 067F8h, 0DBB4h
			dw	01F0Ch, 005FFh, 033AFh, 0031Dh, 0978Dh, 079E4h
			dw	08598h, 0526Bh, 00024h, 0EEFFh, 0E699h, 07175h
			dw	01495h, 05129h, 08DEBh, 0CD92h, 0DAC9h, 0AABBh
			dw	011BEh, 0E454h, 0B1FBh, 01CB0h, 0C6DBh, 03E5Ah
			dw	0C71Ch, 08E90h, 0CB9Bh, 09CE9h, 0D913h, 06FD8h
			dw	0E115h, 0D8C2h, 046FEh, 08C09h, 07870h, 0E434h
			dw	0B10Fh, 06C9Dh, 07B4Eh, 08D72h, 0EDB9h, 0A2BEh
			dw	088D3h, 0CEFDh, 0D785h, 0955Eh, 0BCE3h, 07FFEh
			dw	0913Ah, 0F71Bh, 05F5Ch, 0583Eh, 03792h, 0C5FAh
			dw	0A259h, 0D013h, 0514Ah, 0F62Eh, 0E98Dh, 0D0FBh
			dw	08F5Eh, 0C04Ah, 05FD1h, 0223Dh, 0496Ch, 0637Fh
			dw	067B2h, 08108h, 09B50h, 0101Ah, 0EC8Dh, 09962h
			dw	04386h, 0FF43h, 07476h, 0F42Ah, 0B3F8h, 07369h
			dw	06B75h, 04FEAh, 0C008h, 0E901h, 0841Eh, 05312h
			dw	0BFADh, 00763h, 08804h, 02189h, 0F451h, 09769h
			dw	0C0CAh, 047C0h, 0DD5Dh, 0AEB7h, 0DAF9h, 0A737h
			dw	06348h, 0ACFFh, 0E995h, 065C7h, 062A9h, 02D0Ch
			dw	0E862h, 02AD7h, 04F45h, 0AF00h, 07D15h, 045D3h
			dw	04352h, 06D9Eh, 00F94h, 0EC9Ah, 09690h, 02B70h
			dw	039EBh, 07B84h, 05550h, 00AA0h, 06ABFh, 01A85h
			dw	054ACh, 09326h, 0746Ah, 01112h, 03035h, 0580Eh
			dw	09FCAh, 0CF14h, 0DE0Ah, 02FC4h, 0E455h, 099A4h
			dw	03E7Fh, 0BEBCh, 0B2DAh, 01925h, 02730h, 08E9Eh
			dw	02FB3h, 00127h, 0B1C8h, 058BAh, 0CD39h, 01CF8h
			dw	04786h, 0D6E6h, 006F8h, 00FF7h, 04195h, 00335h
			dw	0C505h, 0673Fh, 021EAh, 04FC0h, 09CB7h, 03B2Bh
			dw	0F7B3h, 0DA75h, 0A291h, 05741h, 00E80h, 0D562h
			dw	0B673h, 03AD7h, 07C77h, 07CC8h, 0FA9Dh, 072A9h
			dw	01DAAh, 07E0Ah, 0CC95h, 005A5h, 0124Eh, 01E20h
			dw	0A876h, 0F02Ah, 0CBBEh, 07E97h, 03125h, 09B30h
			dw	0EA2Fh, 05383h, 0F297h, 06548h, 02C86h, 00754h
			dw	0A5F0h, 0D03Bh, 0F9E5h, 022CFh, 03071h, 001E9h
			dw	09DF5h, 02472h, 06DE7h, 045E8h, 09DBAh, 031BCh
			dw	02C40h, 0F388h, 03F7Dh, 05B50h

DataSeg		ends

; ==========================================================
CodeSeg		segment para public 'code'
		assume	ds: DataSeg

; ----------------------------------------------------------
; Randomize	- Sets up seed based on timer
; ----------------------------------------------------------
; Input:	none
; Output:	none
; ----------------------------------------------------------
Randomize	proc

	push	cx
	push	dx

	call	GetClockTick
	mov	Seed, dx

	pop	dx
	pop	cx

	ret

Randomize	endp


; ----------------------------------------------------------
; GetRandom	- Get random word
; ----------------------------------------------------------
; Input:
;	cx	range
; Output:
;	ax	random value from 0 to range-1
; ----------------------------------------------------------
GetRandom	proc

	push	bx
	push	dx

	mov	ax, Seed

	mov	bx, 0
	mov	bl, RandA
	shl	bx, 1
	add	bx, offset RandomBase
	xor	ax, [bx]

	mov	bx, 0
	mov	bl, RandB
	shl	bx, 1
	add	bx, offset RandomBase
	xor	ax, [bx]

	inc	RandA
	cmp	RandA, 0
	jne	SkipIncRandB

	inc	RandB

SkipIncRandB:
	mov	Seed, ax

	mov	dx, 0
	div	cx
	mov	ax, dx

	pop	dx
	pop	bx
	ret

GetRandom	endp

CodeSeg		ends

		end

